/**
 * 搜索华军软件园
 */

import htt from "../htt"
let headers:any = {
    "sec-ch-ua": `"Microsoft Edge";v="93", " Not;A Brand";v="99", "Chromium";v="93"`,
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "Windows",
    "Upgrade-Insecure-Requests": 1,
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.44"
}

export default function search(name:any){
    // 中文转16进制
    name = "%" + Buffer.from(name).toString("hex").match(/[\w\d]{2}/g).join("%")
    return new Promise(ress=>{
        htt("https").get(`https://www.onlinedown.net/search?type=soft&searchname=${name}&searchsid=win`,{headers},(res)=>{
            switch (res.statusCode){
                case 404 :{
                    ress([])
                    break;
                }
                case 200 :{
                    let html:any = "";
                    res.on("data",(a:any)=>html+=a)
                    res.on("end",()=>{
                        // 分析html
                        html = html.match(/<li class="item">[^]*?<\/li>/g);
                        let list = html?.map((e:string)=>{
                            // 解析出名字 大小 地址
                            return {
                                url: e.match(/href=".*?"/)?e.match(/href=".*?"/)[0].replace(/href="|"/g,"") : null,
                                name:e.match(/<font color='red'>.*?<\/a>/)?e.match(/<font color='red'>.*?<\/a>/)[0].replace(/<font color='red'>|<\/a>|<\/font>|<font color='red'>/g,"") : null,
                                size:e.match(/软件大小：.*?<\/span>/)?e.match(/软件大小：.*?<\/span>/)[0].replace(/软件大小：|<\/span>/g,"") : null,
                                date:e.match(/时间：.*?<\/span>/)?e.match(/时间：.*?<\/span>/)[0].replace(/时间：|<\/span>/g,"") : null
                            }
                        }).filter((e:any)=>e.name)
                        
                        ress(list || [])
                    })
                }
            }
        })
    })
}